<?php                                                                
require_once 'net/crashtoot/php/picasa/viewer/Picasa.class.php';
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Photos');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_Photos_UserQuery');
Zend_Loader::loadClass('Zend_Gdata_Photos_AlbumQuery');
Zend_Loader::loadClass('Zend_Gdata_Photos_PhotoQuery');

class PicasaHandler extends Picasa {

	protected $service;

	public function __construct() {
	}

	public function connect($username, $login, $password) {
		
		try {
			// création du service
			$this->service = new Zend_Gdata_Photos($this->getAuthHttpClient($login, $password), "Google-DevelopersGuide-1.0");

			return true;
		}  catch (Exception $e) {
			echo "Error: connect($username, $login, $password)) : " . $e->getMessage() . "<br />\n"; 
			return false;
		}
	}

	public function getService() {
		return $this->service;
	}
	
	/**
	* Retourne dans un tableau la liste des albums
	*
	* @param unknown_type $client
	* @return array=>array(
	* 	[text]      => titre de l'album
	* 	[thumbnail] => image de l'album
	* 	[albumId]   => identifiant de l'album
	* )
	*/
	public function getAlbums($username, $admin = false) {
		$albums =  array();
		try {
			$query = new Zend_Gdata_Photos_UserQuery();
			$query->setUser($username);

			$userFeed = $this->getService()->getUserFeed(null, $query);
			$cpt = 0;
			foreach ($userFeed as $entry) {
				if(!$admin && $entry->gphotoAccess->text == "private")
					continue;
				
				if ($entry instanceof Zend_Gdata_Photos_AlbumEntry) {
					//print_r($entry);echo "<br><br><hr><br><br>";
					$album = Album::createAlbum($entry);
					$albums[$album->getAlbumId()] = $album;
					$cpt++;
				}
			}
		} catch (Exception $e) {
			echo "Error: getAlbums($username, $admin) : " . $e->getMessage() . "<br />\n"; 
		}
		return $albums;
	}

	/**
	 * Liste les images de l'album.
	 */
	public function getPictures($username, $albumId) {
		$pictures = array();
		
		$query = new Zend_Gdata_Photos_AlbumQuery();
		$query->setUser($username);
		$query->setAlbumId($albumId);     
		// 94, 110, 128, 200, 220, 288, 320, 400, 512, 576, 640,
		// 720, 800, 912, 1024, 1152, 1280, 1440, 1600, d             
    $query->setImgMax("1024");
		
		try {
			$albumFeed = $this->getService()->getAlbumFeed($query);
			//print_r($albumFeed);
			foreach ($albumFeed as $entry) {
				if ($entry instanceof Zend_Gdata_Photos_PhotoEntry) {
					//print_r($entry);echo "<br><br><hr><br><br>";
					$newPhoto = Photo::createPhoto($entry, $username);
					//print_r($newPhoto);echo "<hr>";
					$pictures[$newPhoto->getId()] = $newPhoto;
					//$pictures[$ctp++] = $newPhoto;
				}
			}
		} catch (Exception $e) {
			echo "Error: getPictures($albumId) : " . $e->getMessage() . "<br />\n"; 
		}
		return $pictures;
	}

	/**
	* Outputs an HTML unordered list (ul), with each list item representing a
	* photo in the user's album feed.
	*
	* @param  integer          $albumId The album's id
	* @return void
	*/
	function outputAlbumFeed($albumId, $ulClass = "galerie_mini") {
		$query = new Zend_Gdata_Photos_AlbumQuery();
		$query->setUser($this->username);
		$query->setAlbumId($albumId);
		
		$toEcho = "";
		try {
			$albumFeed = $this->getService()->getAlbumFeed($query);
			$toEcho .= "<h2>Album Feed for: " . $albumFeed->getTitle() . "</h2>";
			$toEcho .= "<ul class='".$ulClass."'>\n";
			foreach ($albumFeed as $entry) {
				if ($entry instanceof Zend_Gdata_Photos_PhotoEntry) {
					$toEcho .= "\t<li class='albums'>";

					$mediaContent = $entry->getMediaGroup()->getContent();
					$toEcho .= "<a href='".$mediaContent[0]->getUrl()."'>";
					$thumb = $entry->getMediaGroup()->getThumbnail();
					$toEcho .= "<img class='thumb' src='".$thumb[1]->getUrl()."' /><br />";
					$toEcho .= $entry->getTitle()."</a>";
					$toEcho .= "</li>\n";
				}
			}
			$toEcho .= "</ul><br />\n";
		} catch (Exception $e) {
			echo "Error: outputAlbumFeed() : " . $e->getMessage() . "<br />\n"; 
		}
		echo $toEcho;
	}

	/**
	* Outputs the feed of the specified photo
	*
	* @param  string           $user    The user's account name
	* @param  integer          $albumId The album's id
	* @param  integer          $photoId The photo's id
	* @return void
	*/
	function outputPhotoFeed($albumId, $photoId, $username = null) {
		
		$query = new Zend_Gdata_Photos_PhotoQuery();
		$query->setUser($username);
		$query->setAlbumId($albumId);
		$query->setPhotoId($photoId);
		$query = $query->getQueryUrl() . "?kind=comment,tag";
		
		$toEcho = "";
		try {
			$photoFeed = $this->getService()->getPhotoFeed($query);
			$toEcho .= "<h2>Photo Feed for: " . $photoFeed->getTitle() . "</h2>";
			// 3 tailles de miniature
			$thumbs = $photoFeed->getMediaGroup()->getThumbnail();
			$toEcho .= "<img src='" . $thumbs[2]->url . "' />";
			
			$toEcho .= "<h3 class='nopad'>Comments:</h3>";
			$toEcho .= "<ul>\n";
			foreach ($photoFeed as $entry) {
				if ($entry instanceof Zend_Gdata_Photos_CommentEntry) {
					$toEcho .= "\t<li>" . $entry->getContent();
					$toEcho .= "<form action='".PicasaUtil::getCurrentScript()."' method='post' class='deleteForm'>";
					$toEcho .= "<input type='hidden' name='user' value='" . $username . "' />";
					$toEcho .= "<input type='hidden' name='album' value='" . $albumId . "' />";
					$toEcho .= "<input type='hidden' name='photo' value='" . $photoId . "' />";
					$toEcho .= "<input type='hidden' name='comment' value='" . $entry->getGphotoId();
					$toEcho .= "' />";
					$toEcho .= "<input type='hidden' name='command' value='deleteComment' />";
					$toEcho .= "<input type='submit' value='Delete' /></form>";
					$toEcho .= "</li>\n";
				}
			}
			$toEcho .= "</ul>\n";
		} catch (Exception $e) {
			echo "Error: outputPhotoFeed() : " . $e->getMessage() . "<br />\n"; 
		}
		echo $toEcho;
	}
}
?>